/*
 * Copyright (c) 2012-2013, Yunnan Yuan Xin technology Co., Ltd.
 * 
 * All rights reserved.
 */
package com.yuanxin.app.ftpfilter.controller.pc;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yuanxin.app.ftpfilter.common.MVCViewName;
import com.yuanxin.app.ftpfilter.service.FtpfilterJob;
import com.yuanxin.framework.logging.Logger;
import com.yuanxin.framework.logging.LoggerFactory;
import com.yuanxin.framework.service.ServiceResult;
import com.yuanxin.framework.util.DateTimeUtils;

/**
 * Hello控制器。
 * 
 */
@Controller
@RequestMapping(value = "/pc/hello")
public class HelloController {

    private static Logger LOG = LoggerFactory.getLogger(HelloController.class);

    @RequestMapping(value = "", method = { RequestMethod.GET, RequestMethod.POST })
    public String hello(Model model,HttpServletRequest request, HttpServletResponse response, HttpSession session){
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setContentType("text/html;charset=utf-8"); 
    	
        LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
        LOG.info("SessionId %s", request.getSession().getId());
        //将当前运用名称传到前端
        model.addAttribute("appId", "ftpfilter");
        model.addAttribute("appName", "");
        return MVCViewName.APP_FTPFILTER_PC_IE9_HELLO_INDEX.toString();
    }
    
    /**
     *异步提交表单
     **/
    @RequestMapping(value = "/submitForm", method = { RequestMethod.GET, RequestMethod.POST })
    public String submitForm(Model model, HttpServletRequest request) {
    	LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
    	LOG.info("SessionId %s", request.getSession().getId());
    	//将当前时间传到前端
    	String start = request.getParameter("startdate");
    	String end = request.getParameter("enddate");
    	model.addAttribute("currentTime", DateTimeUtils.formateDateToStr(new Date(),DateTimeUtils.FORMAT_YMD_HMS));
    	ServiceResult<Object> ret = new ServiceResult<Object>();
    	SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
    	Date s;
    	Date e;
		try {
			s = dft.parse(start);
			e = dft.parse(end);
			bulu(s,e);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    	
    	return "redirect:/pc/hello";
    }
    
    
    public void bulu(Date startDate ,Date endDate) throws Exception {
    	
		Calendar cs = Calendar.getInstance();
		cs.setTime(startDate);
		
		Calendar ce = Calendar.getInstance();
		ce.setTime(endDate);
		SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		while(cs.before(ce)){
			System.out.println(DateTimeUtils.formateDateToStr(cs.getTime(), "yyyy-MM-dd"));
			buludate(cs.getTime());
			cs.set(Calendar.DATE, cs.get(Calendar.DATE) + 1);
		}
		
	}
    public void buludate(Date date) throws Exception {
    	FtpfilterJob ftpfilterJob = new FtpfilterJob();
    	Date d1 = new Date();
    	
    	Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	String month = (c.get(Calendar.MONTH) + 1)<10?"0"+(c.get(Calendar.MONTH) + 1):(c.get(Calendar.MONTH) + 1)+"";
    	String day = (c.get(Calendar.DAY_OF_MONTH))<10?"0"+(c.get(Calendar.DAY_OF_MONTH)):(c.get(Calendar.DAY_OF_MONTH))+"";
    	
    	//PNRD分类
    	String filename = "/pnrd/8L_PNRD_" + c.get(Calendar.YEAR) + month + day +  ".txt";
    	String childpath = "/pnrd";
    	//过滤
    	String filter = "46-48;55-61;63-74;253-286;288-296;303-401";
    	
    	ftpfilterJob.jobutil(filename , childpath , filter);
//		
    	
    	//CPD分类
    	filename = "/cki/8L_CPD_" + c.get(Calendar.YEAR) + month + day +  ".txt";
    	childpath = "/cki";
    	//过滤
    	filter = "158-162;166-174;189-200;222-241;247-319;362-364;371-398";
    	ftpfilterJob.jobutil(filename , childpath , filter);
    	
    	//TKTCRS分类
    	filename = "/tkt/8L_TKTICS_" + c.get(Calendar.YEAR) + month + day +  ".txt";
    	childpath = "/tkt";
    	//过滤
    	Map filterMap = new HashMap();
    	filterMap.put("01", "3-11;26-59;73-92;105-108;165-166;182-198;220-333;340-401");
    	filterMap.put("02", "3-11;26-43;57-167;209-215;249-255;289-295;329-401");
    	filterMap.put("03", "3-11;26-43;67-82;166-215;294-401");
    	filterMap.put("04", "3-11;26-43;84-190;372-401");
    	filterMap.put("05", "3-11;26-43;44-46;83-93;123-132;133-135;172-182;212-221;222-224;261-271;301-310;311-313;350-360;390-401");
    	filterMap.put("07", "3-11;26-43;64-166;187-289;389-401");
    	filterMap.put("08", "3-11;26-43;132-133;220-221;308-309;396-398");
    	filterMap.put("09", "3-11;26-59;78-110;129-161;180-212;231-263;282-314;333-401");
    	filterMap.put("98", "3-11;26-61;121-157;177-381");
    	filterMap.put("11", "3-229");
    	
    	ftpfilterJob.jobutilForFilterMap(filename , childpath , filterMap);
    	
    	//TKTBSP分类
    	filename = "/tkt/8L_TKTBSP_" + c.get(Calendar.YEAR) + month + day +  ".txt";
    	childpath = "/tkt";
    	//过滤
    	filterMap = new HashMap();
    	filterMap.put("01", "3-11;26-59;73-92;105-108;165-166;182-198;220-333;340-401");
    	filterMap.put("02", "3-11;26-43;57-167;209-215;249-255;289-295;329-401");
    	filterMap.put("03", "3-11;26-43;67-82;166-215;294-401");
    	filterMap.put("04", "3-11;26-43;84-190;372-401");
    	filterMap.put("05", "3-11;26-43;44-46;83-93;123-132;133-135;172-182;212-221;222-224;261-271;301-310;311-313;350-360;390-401");
    	filterMap.put("07", "3-11;26-43;64-166;187-289;389-401");
    	filterMap.put("08", "3-11;26-43;132-133;220-221;308-309;396-398");
    	filterMap.put("09", "3-11;26-59;78-110;129-161;180-212;231-263;282-314;333-401");
    	filterMap.put("98", "3-11;26-61;121-157;177-381");
    	filterMap.put("11", "3-229");
    	
    	ftpfilterJob.jobutilForFilterMap(filename , childpath , filterMap);
    	
    	//TKTCRS分类
    	filename = "/tkt/8L_TKTCRS_" + c.get(Calendar.YEAR) + month + day +  ".txt";
    	childpath = "/tkt";
    	//过滤
    	filterMap = new HashMap();
    	filterMap.put("01", "3-11;26-59;73-92;105-108;165-166;182-198;220-333;340-401");
    	filterMap.put("02", "3-11;26-43;57-167;209-215;249-255;289-295;329-401");
    	filterMap.put("03", "3-11;26-43;67-82;166-215;294-401");
    	filterMap.put("04", "3-11;26-43;84-190;372-401");
    	filterMap.put("05", "3-11;26-43;44-46;83-93;123-132;133-135;172-182;212-221;222-224;261-271;301-310;311-313;350-360;390-401");
    	filterMap.put("07", "3-11;26-43;64-166;187-289;389-401");
    	filterMap.put("08", "3-11;26-43;132-133;220-221;308-309;396-398");
    	filterMap.put("09", "3-11;26-59;78-110;129-161;180-212;231-263;282-314;333-401");
    	filterMap.put("98", "3-11;26-61;121-157;177-381");
    	filterMap.put("11", "3-229");
    	
    	ftpfilterJob.jobutilForFilterMap(filename , childpath , filterMap);
    	
    	Date d2 = new Date();
    	System.out.println("执行日期：" + d2.toString() + "--耗时：" + (d2.getTime()-d1.getTime()));
    }
    
}
